﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.Dictionary;
using BCService.Common.Dictionary;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace BCTMSRESTful.Controllers.Management.General.Dictionary
{
    /// <summary>
    /// 字典引用
    /// </summary>
    [Route("api/v1/sim/[controller]")]
    [ApiExplorerSettings(GroupName = "Admin")]
    public class DictionaryController : ManagementBaseController
    {
        #region 私有变量

        private readonly IDictionaryReferenceService dictionaryService;

        #endregion 私有变量

        #region 构造函数

        public DictionaryController(IDictionaryReferenceService dictionaryService)
        {
            this.dictionaryService = dictionaryService;
        }

        #endregion 构造函数

        #region 公共方法

        /// <summary>
        /// 创建索引
        /// </summary>
        /// <response code="200">创建索引成功。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(bool), 200)]
        public async Task<IActionResult> Post([FromBody]DictionaryReferenceRequestDto dto)
        {
            dto.Editer = Admin.FullName;
            var res = await dictionaryService.InsertAsync(dto);

            return Ok(res);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <response code="201">数据修改成功。</response>
        /// <returns></returns>
        [HttpPut("{dictionary_id}")]
        [ProducesResponseType(typeof(bool), 201)]
        public async Task<IActionResult> Put([FromRoute(Name = "dictionary_id")]int dictionaryId, [FromBody]DictionaryReferenceRequestDto dto)
        {
            dto.Editer = Admin.FullName;
            var res = await dictionaryService.UpdateAsync(dictionaryId, dto);

            return StatusCode((int)HttpStatusCode.Created, res);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="dictionaryId">索引id</param>
        /// <response code="204">数据删除成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpDelete("{dictionary_id}")]
        [ProducesResponseType(typeof(bool), 204)]
        public async Task<IActionResult> Delete([FromRoute(Name = "dictionary_id")]int dictionaryId)
        {
            if (dictionaryId == 0)
                return StatusCode((int)HttpStatusCode.BadRequest, "参数 id 不能为0");

            var res = await dictionaryService.DeleteAsync(dictionaryId);
            return StatusCode((int)HttpStatusCode.NoContent, res);
        }

        /// <summary>
        /// 通过id查询字典数据
        /// </summary>
        /// <param name="dictionaryReferenceId">字典id</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("[action]/{dictionaryReference_id}")]
        [ProducesResponseType(typeof(DictionaryReferenceDto), 200)]
        public async Task<IActionResult> Get([FromRoute(Name = "dictionaryReference_id")]int dictionaryReferenceId)
        {
            if (dictionaryReferenceId == 0)
                return StatusCode((int)HttpStatusCode.BadRequest, "DictionaryReferenceId 不能为 0");

            var data = await dictionaryService.GetById(dictionaryReferenceId);

            return Ok(data);
        }

        /// <summary>
        /// 通过索引和索引值类型查询字典
        /// </summary>
        /// <param name="itemValue">索引值</param>
        /// <param name="typeValue">索引类型</param>
        /// <param name="index">起始页</param>
        /// <param name="size">页数量</param>
        /// <param name="ascending">0=降序,1=升序</param>
        /// <response code="200">数据查询成功。</response>
        /// <response code="401">用户无权限访问。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> GetByItemValueAndTypeValue(string itemValue, string typeValue, int index = 1, int size = 10, bool ascending = false)
        {
            index = index == 0 ? 0 : (index - 1) * size;

            var res = await dictionaryService.GetPageableList(index, size, ascending, itemValue, typeValue);

            return Ok(res);
        }

        /// <summary>
        /// 全部字典数据,不带分页
        /// </summary>
        /// <response code="200">数据查询成功。</response>
        /// <response code="204">无内容。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> QueryKeyList()
        {
            var dataList = await dictionaryService.GetAll();

            return Ok(dataList.ToList());
        }

        /// <summary>
        /// 全部字典索引数据,带分页
        /// </summary>
        /// <param name="index">起始页</param>
        /// <param name="size">页数量</param>
        /// <param name="ascending">0=降序,1=升序</param>
        /// <response code="200">数据查询成功。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> Query(int index = 1, int size = 10, bool ascending = false)
        {
            index = index == 0 ? 0 : (index - 1) * size;

            var dataList = await dictionaryService.PageAll(index, size, ascending);

            return Ok(dataList);
        }

        /// <summary>
        /// 通过检索键查询数据
        /// </summary>
        /// <response code="200">数据查询成功。</response>
        /// <param name="searchKey">检索key</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> GetBySearchKey(string searchKey)
        {
            if (string.IsNullOrWhiteSpace(searchKey))
                return StatusCode((int)HttpStatusCode.BadRequest, "searchKey 检索键为空");

            var data = await dictionaryService.GetBySearchkeyAsync(searchKey);
            return Ok(data);
        }

        /// <summary>
        /// 通过引用父名称查询字典
        /// </summary>
        /// <response code="200">数据查询成功。</response>
        /// <param name="parentName">引用父名称</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> GetByParentName([FromQuery(Name = "parent_name")]string parentName)
        {
            var data = await dictionaryService.GetByParentNameAsync(parentName);
            return Ok(data);
        }

        /// <summary>
        /// 通过引用类型名称查询字典
        /// </summary>
        /// <param name="typeName">引用类型名称</param>
        /// <response code="200">数据查询成功。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<DictionaryReferenceDto>), 200)]
        public async Task<IActionResult> GetByTypeName(string typeName)
        {
            var data = await dictionaryService.GetByTypeNameAsync(typeName);
            return Ok(data);
        }
        /// <summary>
        /// 通过父名称与类型获取字典列表
        /// </summary>
        /// <param name="typeName">类型名称</param>
        /// <param name="parentName">父名称</param>
        /// <response code="200">数据查询成功。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<DictionaryReferenceDto>), 200)]
        public IActionResult SearchList([FromRoute(Name = "type_name")]string typeName, [FromRoute(Name = "parent_name")]string parentName)
        {
            var data = dictionaryService.GetByTypeNameAndParentNameAsync(typeName, parentName);
            return Ok(data);
        }
        #endregion 公共方法
    }
}
